

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.ops.composite.array_ops &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.ops.composite.array_ops</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.ops.composite.array_ops</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020-2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;array Operations.&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">mindspore.ops.composite.multitype_ops</span> <span class="kn">import</span> <span class="n">_constexpr_utils</span> <span class="k">as</span> <span class="n">const_utils</span>
<span class="kn">from</span> <span class="nn">mindspore.common</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="kn">from</span> <span class="nn">mindspore.common._register_for_tensor</span> <span class="kn">import</span> <span class="n">tensor_operator_registry</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span> <span class="k">as</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Rel</span>
<span class="kn">from</span> <span class="nn">mindspore.ops.primitive</span> <span class="kn">import</span> <span class="n">constexpr</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">functional</span> <span class="k">as</span> <span class="n">F</span>
<span class="kn">from</span> <span class="nn">..</span> <span class="kn">import</span> <span class="n">operations</span> <span class="k">as</span> <span class="n">P</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_is_int</span><span class="p">(</span><span class="n">arg_value</span><span class="p">,</span> <span class="n">arg_name</span><span class="p">,</span> <span class="n">op_name</span><span class="p">):</span>
    <span class="n">arg_value</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_is_int</span><span class="p">(</span><span class="n">arg_value</span><span class="p">,</span> <span class="n">arg_name</span><span class="p">,</span> <span class="n">op_name</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">arg_value</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_positive_int</span><span class="p">(</span><span class="n">arg_value</span><span class="p">,</span> <span class="n">arg_name</span><span class="p">,</span> <span class="n">op_name</span><span class="p">):</span>
    <span class="n">arg_value</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_positive_int</span><span class="p">(</span><span class="n">arg_value</span><span class="p">,</span> <span class="n">arg_name</span><span class="p">,</span> <span class="n">op_name</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">arg_value</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_axis_range</span><span class="p">(</span><span class="n">arg_value</span><span class="p">,</span> <span class="n">limit</span><span class="p">,</span> <span class="n">arg_name</span><span class="p">,</span> <span class="n">op_name</span><span class="p">):</span>
    <span class="n">arg_value</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_int_range</span><span class="p">(</span><span class="n">arg_value</span><span class="p">,</span> <span class="o">-</span><span class="n">limit</span><span class="p">,</span> <span class="n">limit</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">INC_LEFT</span><span class="p">,</span> <span class="n">arg_name</span><span class="p">,</span> <span class="n">op_name</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">arg_value</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_cal_repeat_dims</span><span class="p">(</span><span class="n">x_rank</span><span class="p">,</span> <span class="n">rep</span><span class="p">,</span> <span class="n">expand_axis</span><span class="p">):</span>
    <span class="n">rep_dims</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">x_rank</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">rep_dims</span><span class="p">[</span><span class="n">expand_axis</span><span class="p">]</span> <span class="o">=</span> <span class="n">rep</span>
    <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">rep_dims</span><span class="p">)</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_cal_reshape</span><span class="p">(</span><span class="n">x_shape</span><span class="p">,</span> <span class="n">rep</span><span class="p">,</span> <span class="n">axis</span><span class="p">):</span>
    <span class="n">x_reshape</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">x_shape</span><span class="p">)</span>
    <span class="n">x_reshape</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span> <span class="o">*=</span> <span class="n">rep</span>
    <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">x_reshape</span><span class="p">)</span>


<div class="viewcode-block" id="repeat_elements"><a class="viewcode-back" href="../../../../api_python/ops/mindspore.ops.repeat_elements.html#mindspore.ops.repeat_elements">[docs]</a><span class="k">def</span> <span class="nf">repeat_elements</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">rep</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Repeat elements of a tensor along an axis, like np.repeat.</span>

<span class="sd">    Args:</span>
<span class="sd">        x (Tensor): The tensor to repeat values for. Must be of type: float16,</span>
<span class="sd">            float32, int8, uint8, int16, int32, or int64.</span>
<span class="sd">        rep (int): The number of times to repeat, must be positive, required.</span>
<span class="sd">        axis (int): The axis along which to repeat, default 0.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        One tensor with values repeated along the specified axis. If x has shape</span>
<span class="sd">        (s1, s2, ..., sn) and axis is i, the output will have shape (s1, s2, ...,</span>
<span class="sd">        si * rep, ..., sn). The output type will be the same as the type of `x`.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor, ops</span>
<span class="sd">        &gt;&gt;&gt; import mindspore</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; # case 1 : repeat on axis 0</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32)</span>
<span class="sd">        &gt;&gt;&gt; output = ops.repeat_elements(x, rep = 2, axis = 0)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [[0 1 2]</span>
<span class="sd">         [0 1 2]</span>
<span class="sd">         [3 4 5]</span>
<span class="sd">         [3 4 5]]</span>
<span class="sd">        &gt;&gt;&gt; # case 2 : repeat on axis 1</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32)</span>
<span class="sd">        &gt;&gt;&gt; output = ops.repeat_elements(x, rep = 2, axis = 1)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [[0 0 1 1 2 2]</span>
<span class="sd">         [3 3 4 4 5 5]]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">const_utils</span><span class="o">.</span><span class="n">check_type_valid</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">mstype</span><span class="o">.</span><span class="n">number_type</span><span class="p">,</span> <span class="s1">&#39;input x&#39;</span><span class="p">)</span>
    <span class="n">rep</span> <span class="o">=</span> <span class="n">_check_positive_int</span><span class="p">(</span><span class="n">rep</span><span class="p">,</span> <span class="s2">&quot;rep&quot;</span><span class="p">,</span> <span class="s2">&quot;repeat_elements&quot;</span><span class="p">)</span>
    <span class="n">axis</span> <span class="o">=</span> <span class="n">_check_is_int</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="s2">&quot;axis&quot;</span><span class="p">,</span> <span class="s2">&quot;repeat_elements&quot;</span><span class="p">)</span>

    <span class="n">shape_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Shape</span><span class="p">()</span>
    <span class="n">rank_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Rank</span><span class="p">()</span>
    <span class="n">tile_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Tile</span><span class="p">()</span>
    <span class="n">expand_dims_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ExpandDims</span><span class="p">()</span>
    <span class="n">reshape_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Reshape</span><span class="p">()</span>

    <span class="n">x_rank</span> <span class="o">=</span> <span class="n">rank_op</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="n">axis</span> <span class="o">=</span> <span class="n">_check_axis_range</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">x_rank</span><span class="p">,</span> <span class="s2">&quot;axis&quot;</span><span class="p">,</span> <span class="s2">&quot;repeat_elements&quot;</span><span class="p">)</span>

    <span class="n">expand_axis</span> <span class="o">=</span> <span class="n">axis</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">x_expand</span> <span class="o">=</span> <span class="n">expand_dims_op</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expand_axis</span><span class="p">)</span>
    <span class="n">rep_dims</span> <span class="o">=</span> <span class="n">_cal_repeat_dims</span><span class="p">(</span><span class="n">x_rank</span><span class="p">,</span> <span class="n">rep</span><span class="p">,</span> <span class="n">expand_axis</span><span class="p">)</span>
    <span class="n">x_expand</span> <span class="o">=</span> <span class="n">tile_op</span><span class="p">(</span><span class="n">x_expand</span><span class="p">,</span> <span class="n">rep_dims</span><span class="p">)</span>
    <span class="n">x_shape</span> <span class="o">=</span> <span class="n">shape_op</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="n">x_reshape</span> <span class="o">=</span> <span class="n">_cal_reshape</span><span class="p">(</span><span class="n">x_shape</span><span class="p">,</span> <span class="n">rep</span><span class="p">,</span> <span class="n">axis</span><span class="p">)</span>
    <span class="n">x_rep</span> <span class="o">=</span> <span class="n">reshape_op</span><span class="p">(</span><span class="n">x_expand</span><span class="p">,</span> <span class="n">x_reshape</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">x_rep</span></div>

<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;repeat_elements&#39;</span><span class="p">,</span> <span class="n">repeat_elements</span><span class="p">)</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_sequence_mask_input_len</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="n">prim_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="n">msg_prefix</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;For &#39;</span><span class="si">{</span><span class="n">prim_name</span><span class="si">}</span><span class="s2">&#39;, the&quot;</span> <span class="k">if</span> <span class="n">prim_name</span> <span class="k">else</span> <span class="s2">&quot;The&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">input_shape</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">msg_prefix</span><span class="si">}</span><span class="s2"> input_shape should be greater than 0, but got </span><span class="si">{</span><span class="n">input_shape</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>
    <span class="c1"># broadcast only supports 7d shape</span>
    <span class="n">shape_size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">input_shape</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">shape_size</span> <span class="o">&gt;=</span> <span class="mi">7</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">msg_prefix</span><span class="si">}</span><span class="s2"> dimension of input_shape should be less than 7, but got </span><span class="si">{</span><span class="n">shape_size</span><span class="si">}</span><span class="s2">d.&quot;</span><span class="p">)</span>


<div class="viewcode-block" id="sequence_mask"><a class="viewcode-back" href="../../../../api_python/ops/mindspore.ops.sequence_mask.html#mindspore.ops.sequence_mask">[docs]</a><span class="k">def</span> <span class="nf">sequence_mask</span><span class="p">(</span><span class="n">lengths</span><span class="p">,</span> <span class="n">maxlen</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">prim_name</span><span class="o">=</span><span class="s1">&#39;sequence_mask&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a mask tensor representing the first N positions of each cell.</span>

<span class="sd">    If lengths has shape [d_1, d_2, ..., d_n], then the resulting tensor mask has type and shape</span>
<span class="sd">    [d_1, d_2, ..., d_n, maxlen], with mask[i_1, i_2, ..., i_n, j] = (j &lt; lengths[i_1, i_2, ..., i_n])</span>

<span class="sd">    Args:</span>
<span class="sd">        lengths (Tensor): Tensor to calculate the mask for. All values in this tensor should be</span>
<span class="sd">            less than or equal to `maxlen`. Values greater than `maxlen` will be treated as `maxlen`.</span>
<span class="sd">        maxlen (int): size of the last dimension of returned tensor. Must be positive and same</span>
<span class="sd">            type as elements in `lengths`. Default is None.</span>
<span class="sd">        prim_name (str): The name of primitive. Default: &#39;sequence_mask&#39;.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **lengths** (Tensor) - Tensor to calculate the mask for. All values in this tensor should be</span>
<span class="sd">          less than or equal to `maxlen`. Values greater than `maxlen` will be treated as `maxlen`.</span>
<span class="sd">          Must be type int32 or int64.</span>
<span class="sd">        - **maxlen** (int) - size of the last dimension of returned tensor. Must be positive and same</span>
<span class="sd">          type as elements in `lengths`. Default is None.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        One mask tensor of shape lengths.shape + (maxlen,).</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `lengths` is not a Tensor.</span>
<span class="sd">        TypeError: If `maxlen` is not an int.</span>
<span class="sd">        TypeError: If dtype of `lengths` is neither int32 nor int64.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``GPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor, ops</span>
<span class="sd">        &gt;&gt;&gt; import mindspore</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; # case 1: When maxlen is assigned</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([1, 2, 3, 4]))</span>
<span class="sd">        &gt;&gt;&gt; output = ops.sequence_mask(x, 5)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [[ True False False False False]</span>
<span class="sd">         [ True  True False False False]</span>
<span class="sd">         [ True  True  True False False]</span>
<span class="sd">         [ True  True  True  True False]]</span>
<span class="sd">        &gt;&gt;&gt; # case 2: When there is 0 in x</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([[1, 3], [2, 0]]))</span>
<span class="sd">        &gt;&gt;&gt; output = ops.sequence_mask(x, 5)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [[[ True False False False False]</span>
<span class="sd">          [ True  True  True False False]]</span>
<span class="sd">         [[ True  True False False False]</span>
<span class="sd">          [False False False False False]]]</span>
<span class="sd">        &gt;&gt;&gt; # case 3: when the maxlen is not assigned</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([[1, 3], [2, 4]]))</span>
<span class="sd">        &gt;&gt;&gt; output = ops.sequence_mask(x)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [[[ True False False False ]</span>
<span class="sd">          [ True  True  True False ]]</span>
<span class="sd">         [[ True  True False False ]</span>
<span class="sd">          [ True  True  True  True ]]]</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">argmax_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ArgMaxWithValue</span><span class="p">()</span>
    <span class="n">reshape_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Reshape</span><span class="p">()</span>
    <span class="n">range_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Range</span><span class="p">()</span>
    <span class="n">expand_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ExpandDims</span><span class="p">()</span>
    <span class="n">cast_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Cast</span><span class="p">()</span>
    <span class="n">shape_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Shape</span><span class="p">()</span>
    <span class="n">to_tensor_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ScalarToArray</span><span class="p">()</span>

    <span class="n">const_utils</span><span class="o">.</span><span class="n">check_type_valid</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">lengths</span><span class="p">),</span> <span class="p">[</span><span class="n">mstype</span><span class="o">.</span><span class="n">int64</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">],</span> <span class="s1">&#39;lengths&#39;</span><span class="p">)</span>
    <span class="n">_check_sequence_mask_input_len</span><span class="p">(</span><span class="n">shape_op</span><span class="p">(</span><span class="n">lengths</span><span class="p">),</span> <span class="n">prim_name</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">maxlen</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">flatten_data</span> <span class="o">=</span> <span class="n">reshape_op</span><span class="p">(</span><span class="n">lengths</span><span class="p">,</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,))</span>
        <span class="n">flatten_data</span> <span class="o">=</span> <span class="n">cast_op</span><span class="p">(</span><span class="n">flatten_data</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">argmax_op</span><span class="p">(</span><span class="n">flatten_data</span><span class="p">)</span>
        <span class="n">maxlen</span> <span class="o">=</span> <span class="n">cast_op</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">maxlen</span> <span class="o">=</span> <span class="n">_check_positive_int</span><span class="p">(</span><span class="n">maxlen</span><span class="p">,</span> <span class="s2">&quot;maxlen&quot;</span><span class="p">,</span> <span class="s2">&quot;sequence_mask&quot;</span><span class="p">)</span>
        <span class="n">maxlen</span> <span class="o">=</span> <span class="n">to_tensor_op</span><span class="p">(</span><span class="n">maxlen</span><span class="p">)</span>

    <span class="n">range_vector</span> <span class="o">=</span> <span class="n">range_op</span><span class="p">(</span><span class="n">to_tensor_op</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">maxlen</span>
                            <span class="p">,</span> <span class="n">to_tensor_op</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
    <span class="n">mask</span> <span class="o">=</span> <span class="n">expand_op</span><span class="p">(</span><span class="n">lengths</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">range_vector</span> <span class="o">&lt;</span> <span class="n">mask</span>
    <span class="k">return</span> <span class="n">result</span></div>

<span class="k">def</span> <span class="nf">masked_fill</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">mask</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
    <span class="n">masked_value</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Fill</span><span class="p">()(</span><span class="n">inputs</span><span class="o">.</span><span class="n">dtype</span><span class="p">,</span> <span class="n">inputs</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">P</span><span class="o">.</span><span class="n">Select</span><span class="p">()(</span><span class="n">mask</span><span class="p">,</span> <span class="n">masked_value</span><span class="p">,</span> <span class="n">inputs</span><span class="p">)</span>

<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;masked_fill&#39;</span><span class="p">,</span> <span class="n">masked_fill</span><span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>